var Back = 0;
var Next = 1;
var g_MainSliderTimingID;
$(document).ready(function(){
	main_init();
});


function main_init()
{
	$("#main_slider_container").css('background-image',"url(./image/main-sider-pic-1.jpg)");
	register_button_click();
	register_event();
	g_MainSliderTimingID = setInterval(main_slider_handle,3000);
}

function register_button_click() {
	$("#prev_slider_pic").click(
		function() {
			main_slider_pic_change(Back);
		});

	$("#next_slider_pic").click(
		function() {
			main_slider_pic_change(Next);
		});

}

function register_event(){

}

function main_slider_handle()
{
	main_slider_pic_change(Next);
}

function parse_filename_from_url(pic_url)
{
	console.log("pic_url:"+pic_url);
	var filename = "";
	var start_pos = pic_url.lastIndexOf("/");
	var end_pos = pic_url.lastIndexOf("\"");
	
	filename = pic_url.slice(start_pos+1,end_pos);

	return filename;
}

function main_slider_pic_change(changeDirection)
{
	clearInterval(g_MainSliderTimingID);
	var pic_path = $("#main_slider_container").css('background-image');
	var picFileName = parse_filename_from_url(pic_path);
	var nextPicFileName = "";

	if ( 0 == changeDirection )
	{
		nextPicFileName = get_prev_slider_pic_name(picFileName);
	}
	else
	{
		nextPicFileName = get_next_slider_pic_name(picFileName);
	}

	pic_path = pic_path.replace(picFileName,nextPicFileName);
	$("#main_slider_container").css('background-image',pic_path);	
	g_MainSliderTimingID = setInterval(main_slider_handle,3000);
}

function get_prev_slider_pic_name(currPicFileName)
{
	var retPicFileName = "";
	switch(currPicFileName)
	{
		case "main-sider-pic-1.jpg":
			retPicFileName = "main-sider-pic-3.jpg";
		break;
		
		case "main-sider-pic-2.jpg":
			retPicFileName = "main-sider-pic-1.jpg";
		break;

		case "main-sider-pic-3.jpg":
			retPicFileName = "main-sider-pic-2.jpg";
		break;
	}

	return retPicFileName;
}

function get_next_slider_pic_name(currPicFileName)
{
	var retPicFileName = "";
	switch(currPicFileName)
	{
		case "main-sider-pic-1.jpg":
			retPicFileName = "main-sider-pic-2.jpg";
		break;
		
		case "main-sider-pic-2.jpg":
			retPicFileName = "main-sider-pic-3.jpg";
		break;

		case "main-sider-pic-3.jpg":
			retPicFileName = "main-sider-pic-1.jpg";
		break;
	}

	return retPicFileName;
}